2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucer_Headers.h"
27 #include "jucer_JucerDocument.h"
30 //==============================================================================
31 BinaryResources::BinaryResources()
35 BinaryResources::~BinaryResources()
39 BinaryResources
& BinaryResources::operator= (const BinaryResources
& other
)
41 for (int i
= 0; i
< other
.resources
.size(); ++i
)
42 add (other
.resources
[i
]->name
,
43 other
.resources
[i
]->originalFilename
,
44 other
.resources
[i
]->data
);
49 void BinaryResources::changed()
54 document
->refreshAllPropertyComps();
58 //==============================================================================
59 void BinaryResources::clear()
61 if (resources
.size() > 0)
68 const StringArray
BinaryResources::getResourceNames() const
72 for (int i
= 0; i
< resources
.size(); ++i
)
73 s
.add (resources
.getUnchecked(i
)->name
);
78 BinaryResources::BinaryResource
* BinaryResources::findResource (const String
& name
) const throw()
80 for (int i
= resources
.size(); --i
>= 0;)
81 if (resources
.getUnchecked(i
)->name
== name
)
82 return resources
.getUnchecked(i
);
87 const BinaryResources::BinaryResource
* BinaryResources::getResource (const String
& name
) const
89 return findResource (name
);
92 const BinaryResources::BinaryResource
* BinaryResources::getResourceForFile (const File
& file
) const
94 for (int i
= resources
.size(); --i
>= 0;)
95 if (resources
.getUnchecked(i
)->originalFilename
== file
.getFullPathName())
96 return resources
.getUnchecked(i
);
101 bool BinaryResources::add (const String
& name
, const File
& file
)
105 if (! file
.loadFileAsData (mb
))
108 add (name
, file
.getFullPathName(), mb
);
112 void BinaryResources::add (const String
& name
, const String
& originalFileName
, const MemoryBlock
& data
)
114 BinaryResource
* r
= findResource (name
);
118 resources
.add (r
= new BinaryResource());
122 r
->originalFilename
= originalFileName
;
124 deleteAndZero (r
->drawable
);
129 bool BinaryResources::reload (const int index
)
131 return resources
[index
] != 0
132 && add (resources
[index
]->name
,
133 File (resources
[index
]->originalFilename
));
136 const String
BinaryResources::browseForResource (const String
& title
,
137 const String
& wildcard
,
138 const File
& fileToStartFrom
,
139 const String
& resourceToReplace
)
141 FileChooser
fc (title
, fileToStartFrom
, wildcard
);
143 if (fc
.browseForFileToOpen())
145 String
name (resourceToReplace
);
148 name
= findUniqueName (fc
.getResult().getFileName());
150 if (! add (name
, fc
.getResult()))
152 AlertWindow::showMessageBox (AlertWindow::WarningIcon
,
153 TRANS("Adding Resource"),
154 TRANS("Failed to load the file!"));
156 name
= String::empty
;
162 return String::empty
;
165 const String
BinaryResources::findUniqueName (const String
& rootName
) const
167 String
nameRoot (makeValidCppIdentifier (rootName
, true, true, false));
168 String
name (nameRoot
);
170 const StringArray
resources (getResourceNames());
174 while (resources
.contains (name
))
175 name
= nameRoot
+ String (++suffix
);
180 void BinaryResources::remove (const int i
)
182 if (resources
[i
] != 0)
184 resources
.remove (i
);
189 const Drawable
* BinaryResources::getDrawable (const String
& name
) const
191 BinaryResources::BinaryResource
* const res
= const_cast <BinaryResources::BinaryResource
*> (getResource (name
));
196 if (res
->drawable
== 0 && res
->data
.getSize() > 0)
197 res
->drawable
= Drawable::createFromImageData (res
->data
.getData(), res
->data
.getSize());
199 return res
->drawable
;
202 const Image
BinaryResources::getImageFromCache (const String
& name
) const
204 const BinaryResources::BinaryResource
* const res
= getResource (name
);
206 if (res
!= 0 && res
->data
.getSize() > 0)
207 return ImageCache::getFromMemory (res
->data
.getData(), (int) res
->data
.getSize());
212 void BinaryResources::loadFromCpp (const File
& cppFileLocation
, const String
& cppFile
)
215 cpp
.addLines (cppFile
);
219 for (int i
= 0; i
< cpp
.size(); ++i
)
221 if (cpp
[i
].contains ("JUCER_RESOURCE:"))
224 tokens
.addTokens (cpp
[i
].fromFirstOccurrenceOf (":", false, false), ",", "\"'");
226 tokens
.removeEmptyStrings();
228 const String
resourceName (tokens
[0]);
229 const int size
= tokens
[1].getIntValue();
230 const String
originalFileName (cppFileLocation
.getSiblingFile (tokens
[2].unquoted()).getFullPathName());
232 jassert (resourceName
.isNotEmpty() && size
> 0);
234 if (resourceName
.isNotEmpty() && size
> 0)
236 const int firstLine
= i
;
238 while (i
< cpp
.size())
239 if (cpp
[i
++].contains ("}"))
242 const String
dataString (cpp
.joinIntoString (" ", firstLine
, i
- firstLine
)
243 .fromFirstOccurrenceOf ("{", false, false));
245 MemoryOutputStream out
;
246 String::CharPointerType
t (dataString
.getCharPointer());
249 while (! t
.isEmpty())
251 const juce_wchar c
= t
.getAndAdvance();
253 if (c
>= '0' && c
<= '9')
254 n
= n
* 10 + (c
- '0');
257 out
.writeByte ((char) n
);
264 jassert (size
< (int) out
.getDataSize() && size
> (int) out
.getDataSize() - 2);
266 MemoryBlock
mb (out
.getData(), out
.getDataSize());
269 add (resourceName
, originalFileName
, mb
);
275 //==============================================================================
276 void BinaryResources::fillInGeneratedCode (GeneratedCode
& code
) const
278 if (resources
.size() > 0)
280 code
.publicMemberDeclarations
<< "// Binary resources:\n";
283 defs
<< "//==============================================================================\n";
284 defs
<< "// Binary resources - be careful not to edit any of these sections!\n\n";
286 for (int i
= 0; i
< resources
.size(); ++i
)
288 code
.publicMemberDeclarations
289 << "static const char* "
290 << resources
[i
]->name
291 << ";\nstatic const int "
292 << resources
[i
]->name
295 const String
name (resources
[i
]->name
);
296 const MemoryBlock
& mb
= resources
[i
]->data
;
298 defs
<< "// JUCER_RESOURCE: " << name
<< ", " << (int) mb
.getSize()
300 << File (resources
[i
]->originalFilename
)
301 .getRelativePathFrom (code
.document
->getFile())
302 .replaceCharacter ('\\', '/')
306 line1
<< "static const unsigned char resource_"
307 << code
.className
<< "_" << name
<< "[] = { ";
311 MemoryOutputStream
out (65536);
312 int charsOnLine
= line1
.length();
314 for (size_t j
= 0; j
< mb
.getSize(); ++j
)
316 const int num
= ((int) (unsigned char) mb
[j
]);
325 if (charsOnLine
>= 200)
335 << (const char*) out
.getData()
336 << "0,0};\n\nconst char* "
337 << code
.className
<< "::" << name
338 << " = (const char*) resource_" << code
.className
<< "_" << name
340 << code
.className
<< "::" << name
<< "Size = "
341 << (int) mb
.getSize()
345 code
.staticMemberDefinitions
+= defs
;
349 BinaryResources::BinaryResource::BinaryResource()
354 BinaryResources::BinaryResource::~BinaryResource()
356 deleteAndZero (drawable
);